package com.xjz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjz.common.enums.ErrorCode;
import com.xjz.exception.BusinessException;
import com.xjz.model.entity.Post;
import com.xjz.model.entity.PostFavour;
import com.xjz.model.entity.User;
import com.xjz.service.PostFavourService;
import com.xjz.mapper.PostFavourMapper;
import com.xjz.service.PostService;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
* @author 86135
* @description 针对表【post_favour(帖子收藏)】的数据库操作Service实现
* @createDate 2025-08-02 15:49:55
*/
@Service
public class PostFavourServiceImpl extends ServiceImpl<PostFavourMapper, PostFavour>
    implements PostFavourService{

    @Resource
    private PostService postService;

    @Resource
    private PostFavourMapper postFavourMapper;

    /**
     * 帖子收藏 | 取消收藏
     * @param postId
     * @param loginUser
     * @return
     */
    @Override
    public Integer doPostFavour(Long postId, User loginUser) {
        Post post = postService.getById(postId);
        if (post == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }

        //是否已收藏帖子
        Long userId = loginUser.getId();
        //每个用户串行帖子收藏
        //锁必须要包裹住整个事务方法
        PostFavourService postFavourService = (PostFavourService) AopContext.currentProxy();
        synchronized (String.valueOf(userId).intern()) {
            return postFavourService.doPostFavourInner(userId,postId);
        }
    }

    /**
     * 收藏/取消收藏接口
     * @param userId
     * @param postId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer doPostFavourInner(Long userId, Long postId) {

        Post post = postService.getById(postId);
        if (post == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }

        PostFavour postFavour = new PostFavour();
        postFavour.setUserId(userId);
        postFavour.setPostId(postId);

        LambdaQueryWrapper<PostFavour> lambdaQueryWrapper = new LambdaQueryWrapper<>(postFavour);

        PostFavour oldPostFavour = getOne(lambdaQueryWrapper);

        boolean result;
        //收藏过
        if (oldPostFavour != null) {
            result = remove(lambdaQueryWrapper);
            if (result) {
                result = postService.update()
                        .eq("id", postId)
                        .gt("favourNum", 0)
                        .setSql("favourNum = favourNum - 1")
                        .update();
                return result ? -1 : 0;
            } else {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        } else {
            result  = save(postFavour);
            if (result) {
                result = postService.update()
                        .eq("id", postId)
                        .setSql("favourNum = favourNum + 1")
                        .update();
                return result ? 1 : 0;
            } else {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        }
    }

    /**
     * 获取我收藏的帖子列表
     * @param page
     * @param queryWrapper
     * @param userId
     * @return
     */
    @Override
    public Page<Post> listFavourPostByPage(Page<Post> page, QueryWrapper<Post> queryWrapper, Long userId) {
        if (userId <= 0) {
            return new Page<>();
        }
        return postFavourMapper.listFavourPostByPage(page,queryWrapper,userId);
    }
}




